package com.heima.mapper;

import com.heima.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface UserMapper {

    /**
     * 根据手机号查询账户信息
     */
    User selectByMobile(String mobile);

    @Select("select * from user where mobile = #{name} and password = #{password}")
    Integer loginIn(@Param("name") String name,@Param("password") String password);

    @Select("SELECT " +
            " u.*,s.groupsIntegral " +
            " FROM " +
            " USER u " +
            " LEFT JOIN ( SELECT group_id, sum( integral ) groupsIntegral FROM USER " +
            " WHERE class_id = #{classId} GROUP BY group_id ) s ON s.group_id = u.group_id" +
            " WHERE role_id = '2'")
    List<User> findByClassId(Integer classId);


    @Update("UPDATE user set integral = #{integral} where id = #{id}")
    Integer addIntegral(@Param("integral") Integer integral,@Param("id") Integer id);

    @Select("select * from user where id = #{id}")
    User findById(Integer id);


}
